package com.syt.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syt.commonutils.R;
import com.syt.commonutils.RandomUtil;
import com.syt.entity.UcenterMemberVO;
import com.syt.staservice.entity.StatisticsDaily;
import com.syt.staservice.feignclient.UcenterClient;
import com.syt.staservice.mapper.StatisticsDailyMapper;
import com.syt.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author SYT
 * @since 2021-03-16
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {
        //添加记录之前删除表中相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        //远程调用共得到某一天调用人数
        R registerR = ucenterClient.countRegister(day);
        Integer count = (Integer) registerR.getData().get("count");
        //把获取数据添加到数据库,统计分析表中
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        List<String> DataCalculated = list.stream().map(StatisticsDaily::getDateCalculated).collect(Collectors.toList());
        List<Integer> count = null;
        switch (type){
            case "login_num":
                count = list.stream().map(StatisticsDaily::getLoginNum).collect(Collectors.toList());
                break;
            case "register_num":
                count = list.stream().map(StatisticsDaily::getRegisterNum).collect(Collectors.toList());
                break;
            case "video_view_num":
                count = list.stream().map(StatisticsDaily::getVideoViewNum).collect(Collectors.toList());
                break;
            case "course_num":
                count = list.stream().map(StatisticsDaily::getCourseNum).collect(Collectors.toList());
                break;
            default:break;
        }
        HashMap<String,Object> map =new HashMap<>();
        map.put("date_calculatedList",DataCalculated);
        map.put("numDataList",count);
        return map;
    }
}
